package com.tomato.thread.pool;


import java.text.MessageFormat;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class DiscardPolicy {

    private final static AtomicInteger atomic = new AtomicInteger(1);


    public static void main(String[] args) {
        final Integer ACPU = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(
                        2 * ACPU + 1,
                        2 * ACPU + 1,
                        0,
                        TimeUnit.SECONDS,
                        new LinkedBlockingDeque<>(6),
                        new ThreadPoolExecutor.DiscardPolicy());
        for (int i = 0; i <= 100; i++) {
            executor.execute(() -> {
                try {
                    // 模拟耗时
                    Thread.sleep(100);
                    System.out.println(MessageFormat.format("{0}:执行{1}个任务完成", Thread.currentThread().getName(), atomic.getAndIncrement()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
